Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

253
Vistas
Cannot read properties of undefined "collectionName" - Mongoose.models

I'm trying to data in the Note collection from a local database of MongoDB using Mongoose with Nextjs and getting an error "Cannot read properties of undefined (reading 'note')"

Any ideas on what I am doing wrong?

import mongoose from 'mongoose';

const { Schema } = mongoose;
const NoteSchema = new Schema(
   {
      title: {
         type: String,
         maxlength: 60,
      },
      description: {
         type: String,
         maxlength: 200,
      },
   },
   { timestamps: true }
);

export default mongoose.models.note || mongoose.model('note', NoteSchema)

import dbConnect from "../../../lib/dbConnect"
import Note from "../../../models/Note"

export default async function handler(req, res) {
    const { method } = req
    
    await dbConnect()

    if (method === 'GET') {

        try {
            const note = await Note.find({}) 

            res.status(200).json({ success: true, data: note})

          } catch (error) {
            res.status(400).json({ success: false })
          }
        
    }

    if (method === 'POST') {

        try {

            const note = await Note.create(req.body)
            
            res.status(200).json({ success: true, data: note})
            
        } catch (error) {

            res.status(500).json(error)
            
        }
        
    }
}
import mongoose from 'mongoose'

const {MONGODB_URI} = process.env




let cached = global.mongoose

if (!cached) {
  cached = global.mongoose = { conn: null, promise: null }
}

async function dbConnect() {
  if (cached.conn) {
    return cached.conn
  }

  if (!cached.promise) {
    const opts = {
      useNewUrlParser: true,
      useUnifiedTopology:true,
      bufferCommands: false,
    }

    cached.promise = mongoose.connect(MONGODB_URI, opts).then((mongoose) => {
      return mongoose
    })
  }
  cached.conn = await cached.promise
  return cached.conn
}

export default dbConnect

I get the following

Unhandled Runtime Error TypeError: Cannot read properties of undefined (reading 'note')

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

I was able to fix it by changing the export module

global.PizzaSchema = global.NoteSchema || mongoose.model('Note', NoteSchema);
export default global.NoteSchema;
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda